package com.choosefine.it.company.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.base.SendPush;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.service.CountService;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.company.dao.CompanyPublishprojectMapper;
import com.choosefine.it.company.model.CompanyPublishproject;
import com.choosefine.it.company.model.vo.CompanyPublishManagerVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
/**
 * @author chenjian
 * @date 2016-12-2 上午9:17:08
 */
@Service("companyPublishprojectService")
public class CompanyPublishprojectService extends BaseService {
	@Autowired
	CompanyPublishprojectMapper companyPublishprojectMapper;
	
	@Autowired
	MongoTemplate mongoTemplate;
	
	@Autowired
	CustomRestTemplate restTemplate;

	@Autowired
	CountService countService;
	
	@Autowired
	SendPush sendPush;
	
	public int saveCompanyPublishproject(CompanyPublishproject record) {
		//userCode 校验
		companyInfoUserCodeCheck(record.getUserCode());
		//根据usercode自动获取发布人以及项目承包人（发布人和项目承包人是一个人）
		record.setCtime(DateUtils.getNowTime());
		record.setPublishTime(DateUtils.getNowTime());
		record.setIsValid(0);
		record.setPublishStatus(0);
		record.setReleaseNo("CPP"+ System.currentTimeMillis());
		if (!StringUtils.isEmpty(record.getPublisher())){
			record.setVerifyStatus(CommonConstant.VERIFY_STATUS_1);
		} else {
			record.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		}
		int num = companyPublishprojectMapper.saveCompanyPublishproject(record);
		if (num > 0){
			mongoTemplate.insert(record);
		}
		return num;
	}
	
	public int updateCompanyPublishinfo(CompanyPublishproject record){
		int num = companyPublishprojectMapper.updateCompanyPublishinfo(record);
		if (num > 0){
			Update update = mongoUpdateCompanyPublishProject(record);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(record.getId())), update, CompanyPublishproject.class);
		}
		return num;
	}
	private Update mongoUpdateCompanyPublishProject(CompanyPublishproject record) {
		Update update = new Update();
			update.set("cityCode", record.getCityCode());
			update.set("cityName", record.getCityName());
			update.set("provinceCode", record.getProvinceCode());
			update.set("provinceName", record.getProvinceName());
			update.set("constructUnit", record.getConstructUnit());
			update.set("contact", record.getContact());
			update.set("phone1", record.getPhone1());
			update.set("phone2", record.getPhone2());
			update.set("projectName", record.getProjectName());
			update.set("projectTypeId", record.getProjectTypeId());
			update.set("projectTypeName", record.getProjectTypeName());
			update.set("startTime", record.getStartTime());
			update.set("timeLimit", record.getTimeLimit());
			update.set("utime", record.getUtime());
			update.set("projectAddress", record.getProjectAddress());
			update.set("publishExplain", record.getPublishExplain());
			update.set("publisher", record.getPublisher());
			update.set("publishTime", record.getPublishTime());
			update.set("verifyStatus", record.getVerifyStatus());
			update.set("constructionUnit", record.getConstructionUnit());
			update.set("constructUnit", record.getConstructUnit());
			update.set("constructionUnitCode", record.getConstructionUnitCode());
			update.set("constructUnitCode", record.getConstructUnitCode());
			update.set("reason", record.getReason());
			update.set("projectStandard", record.getProjectStandard());
		return update;
	}
	
	
	public int updateFindProjectInfoPublishstatusByID(Map<String, Object> params) {
		int num = companyPublishprojectMapper.updateFindProjectInfoPublishstatusByID(params);
		if (num > 0){
			Update update = new Update();
			update.set("publishStatus", params.get("publishStatus"));
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, CompanyPublishproject.class);
		}
		return num;
	}
	/*   yuxin3.08
	public CompanyPublishProjectInfoVO findCompanyPublishProjectInfoById(Integer publishId) {
		return companyPublishprojectMapper.findCompanyPublishProjectInfoById(publishId);
	}*/
	/* (non-Javadoc)
	 * @see com.choosefine.it.company.service.CompanyPublishprojectService#selectCompanyFindManagerByCondition(java.util.Map, com.choosefine.it.dto.PageDTO)
	 */
	
	public Page<CompanyPublishManagerVo> selectCompanyFindManagerByCondition(Map<String, Object> params,
			PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<CompanyPublishManagerVo> page = companyPublishprojectMapper.selectCompanyFindManagerByCondition(params);

		return page;
	}
	/* (non-Javadoc)
	 * @see com.choosefine.it.company.service.CompanyPublishprojectService#updateVerifyStatus(java.util.Map)
	 */
	
	public int updateVerifyStatus(Map<String, Object> params) {
		int num=0;
		List<Integer> ids = new ArrayList<>();
		num=companyPublishprojectMapper.updateVerifyStatus(params);
		if(num>0){
			Update update = new Update();
			update.set("verifyStatus", params.get("verifyStatus"));
			update.set("publisher", params.get("publisher"));
			update.set("utime", params.get("utime"));
			if (params.containsKey("reason")){
				update.set("reason", params.get("reason"));
			}
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, CompanyPublishproject.class);
			//对用户进行推送审核结果的通知
//			ArrayList<Integer> idss=(ArrayList<Integer>) params.get("ids");
//			Integer verifyStatus= (Integer) params.get("verifyStatus");
//			for(Integer id:idss){
//			CompanyPublishproject companyPublishproject=companyPublishprojectMapper.findCompanyPublishProjectById(id);
//			if(null!=companyPublishproject){
//				String userCode=companyPublishproject.getUserCode();
//				if(verifyStatus==1){
//					sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISVERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISVERIFY);
//				}else{
//					sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISNOT_VERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISNOT_VERIFY);
//				}
//			}
//			}
			
		}
		return num;
	}
	/* (non-Javadoc)
	 * @see com.choosefine.it.company.service.CompanyPublishprojectService#deleteFindManager(java.util.Map)
	 */
	
	public int deleteFindManager(Map<String, Object> params) {
		int num = companyPublishprojectMapper.deleteFindManager(params); 
		if (num > 0){
			List<Integer> list = (List<Integer>) params.get("ids");
			for (Integer id:list){
				Update update = new Update();
				update.set("utime", params.get("utime"));
				update.set("isValid", CommonConstant.INVALID);
				mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)), update, CompanyPublishproject.class);
			}
		}
		return num;
	}
	/*项目经理查询项目的详情yuxin3.11*/
	
	public CompanyPublishproject getDetails(Map<String, Object> params){
		CompanyPublishproject companyPublishproject=mongoTemplate.findOne(new Query(Criteria.where("_id").is(params.get("publishId"))),CompanyPublishproject.class);
		int isInsert=0;
		if(companyPublishproject!=null){
			isInsert=countService.isInsert(params);
			companyPublishproject.setIsInsert(isInsert);
		}
		return companyPublishproject;
	}

}
